//
//  FYEmoticonCollectionViewCell.swift
//  Mircore
//
//  Created by apple on 16/8/17.
//  Copyright © 2016年 飞羽. All rights reserved.
//

import UIKit

//表情视图自定义cell
class FYEmoticonCollectionViewCell: UICollectionViewCell {
    
    //表情数组模型
    var   emoticons: [FYEmoticon]? {
    
        didSet  {
         
            guard  let  ets   =  emoticons  else  {
            
              return
            
            }
            //默认情况下表情按钮都隐藏  设置数据的时候再让其显示出来
            
            for value  in emoticonButtonArray {
                
                //隐藏表情
                
                value.hidden = true
            }
            
            
            //绑定表情数据
            
            for(i, value)  in ets.enumerate()  {
            
            //获取表情按钮的控件
                
                let  emoticonButton = emoticonButtonArray[i]
                
                //绑定数据显示表情按钮
                
                emoticonButton.hidden = false
                
                //绑定表情模型
                 emoticonButton.emoticon  = value
                
                //如果是0表示设置图片
                
                if  value.type  == "0" {
                
                emoticonButton.setImage(UIImage(named: value.path!), forState: .Normal)
                
                    
                    //设置图片不需要设置文字
                    
                    emoticonButton.setTitle(nil, forState: .Normal)
                
                }else  {
                
                //设置emoji
                    
                    emoticonButton.setTitle((value.code! as NSString).emoji(), forState: .Normal)
                    
                    //设置emoji不需要设置图片
                
                   emoticonButton.setImage(nil, forState: .Normal)
                }
            
             }
            
         }
    
     }
    
    
    //记录创建的20个表情按钮
    
    private  lazy  var  emoticonButtonArray: [FYEmoticonButton] = [FYEmoticonButton]()
    
    
    //删除表情按钮
    
    private lazy  var  deleteButton: UIButton = {
    
      let  button  = UIButton()
        
        //添加点击事件
        
        button.addTarget(self, action: #selector(FYEmoticonCollectionViewCell.deleteButtonAction), forControlEvents: .TouchUpInside)
        
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: .Normal)
        
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: .Highlighted)
        
        return button
    
    }()
    
     override  init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required  init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder: ) has  not been implemented")
    }
    
    private func  setupUI() {
    
     //添加表情按钮
        
        addChildButton()
        
        //添加删除表情按钮
    
        contentView.addSubview(deleteButton)
    }
    
    
    
    //添加表情按钮
    
    private func  addChildButton()  {
    
    //遍历20次创建表情按钮
        
        for _ in 0..<20 {
            
            let  button  = FYEmoticonButton()
            
            //添加点击事件
            
            button.addTarget(self, action: #selector(FYEmoticonCollectionViewCell.emoticonButtonAction(_:)), forControlEvents: .TouchUpInside)
            //设置字体大小
            
            
            
            button.titleLabel?.font  = UIFont.systemFontOfSize(34)
            
            contentView.addSubview(button)
            
            //存储到数组里面
            
            emoticonButtonArray.append(button)
        }
    
     }
    
    //MARK:-点击删除按钮的事件
    
    @objc  private  func  deleteButtonAction() {
    
    
    NSNotificationCenter.defaultCenter().postNotificationName(DidSelectedDeleteEmoticonNotification, object: nil)
     
    }
    
    //MARK:-  点击表情按钮事件
    
    @objc  private  func    emoticonButtonAction(button: FYEmoticonButton)  {
    
      let  emoticon  = button.emoticon
    
    
        //发送通知
        
        NSNotificationCenter.defaultCenter().postNotificationName(DidSelectedEmoticonNotification, object: emoticon)
    }
    
    
    //给表情按钮布局
    
    override func  layoutSubviews() {
        
        super.layoutSubviews()
        
        //计算表情按钮的宽度
        
        let  itemWidth  = width  / 7
        
        //计算表情按钮的高度
        
        let  itemHeight  = height  / 3
        
        for (i, value) in emoticonButtonArray.enumerate() {
            
            
            //设置大小
            
            value.size  = CGSize(width: itemWidth, height: itemHeight)
            
            //计算当前的列索引
            
            let   colIndex = i % 7
            
            //计算当前的行索引
            
            let  rowIndex = i / 7
            
            
            //设置x坐标和y坐标
            
            value.x = CGFloat(colIndex)   * itemWidth
            
            value.y  = CGFloat(rowIndex)  * itemHeight
            
         }
        
        //设置删除按钮的大小
        
        deleteButton.size  = CGSize(width: itemWidth, height: itemHeight)
        
        //设置x,y坐标
        
        deleteButton.x  = width  - itemWidth
        
        deleteButton.y  = height - itemHeight
    }
}
